package cn.com.hhrcw.web;

import cn.com.hhrcw.entity.OpJob;
import cn.com.hhrcw.entity.StatisticsArea;
import cn.com.hhrcw.entity.StatisticsIndustry;
import cn.com.hhrcw.mapstruct.StatisticsAreaConvert;
import cn.com.hhrcw.params.StatParams;
import cn.com.hhrcw.params.StatisticsAreaParams;
import cn.com.hhrcw.service.IStatisticsAreaService;
import cn.com.hhrcw.utils.ExcelUtil;
import cn.com.hhrcw.vo.StatisticsAreaVO;
import cn.com.hhrcw.vo.StatisticsIndustryVO;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.aspect.annotation.PermissionData;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.base.entity.AttributesValueParams;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;
import java.util.function.Function;

/**
 * 数据报表-地区招聘数据
 *
 * @author jiangdong
 * @since 2020-11-19
 */
@RestController
@Slf4j
@RequestMapping("/statisticsArea")
@Api(tags = "")
@Validated
public class StatisticsAreaController extends JeecgController<StatisticsArea, IStatisticsAreaService> {

    @Autowired
    private IStatisticsAreaService statisticsAreaService;

    @Autowired
    private StatisticsAreaConvert statisticsAreaConvert;


    /**
     * 地区统计数据
     *
     * @param params 请求参数
     * @return
     */
    @GetMapping(value = "/list")
    @AutoLog(value = "地区数据统计",logType = 2,ty = "visit")
    @ApiOperation(value = "分页列表查询", notes = "分页列表查询")
    @PermissionData
    public Result<Page<StatisticsAreaVO>> list(StatParams params, HttpServletRequest request) {
        LambdaQueryWrapper<StatisticsArea> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.ne(StatisticsArea::getLevel, "province");
        if (org.apache.commons.lang3.StringUtils.isNotBlank(params.getId())) {
            if (StringUtils.isNotBlank(params.getId())) {
                String[] id = params.getId().split(",");
                queryWrapper.eq(StatisticsArea::getId, id[1]);
            }
        }
        if (StringUtils.isNotBlank(params.getName())) {
            queryWrapper.like(StatisticsArea::getName, params.getName());
        }
        if (params.getReCountMin() != null) {
            queryWrapper.ge(StatisticsArea::getResumeCount, params.getReCountMin());
        }
        if (params.getReCountMax() != null) {
            queryWrapper.le(StatisticsArea::getResumeCount, params.getReCountMax());
        }
        if (params.getFeedbackRateMin() != null) {
            queryWrapper.ge(StatisticsArea::getFeedbackRate, params.getFeedbackRateMin());
        }
        if (params.getFeedbackRateMax() != null) {
            queryWrapper.le(StatisticsArea::getFeedbackRate, params.getFeedbackRateMax());
        }
        if (params.getViewRateMin() != null) {
            queryWrapper.ge(StatisticsArea::getViewRate, params.getViewRateMin());
        }
        if (params.getViewRateMax() != null) {
            queryWrapper.le(StatisticsArea::getViewRate, params.getViewRateMax());
        }
        if (params.getEntryRateMin() != null) {
            queryWrapper.ge(StatisticsArea::getEntryRate, params.getEntryRateMin());
        }
        if (params.getEntryRateMax() != null) {
            queryWrapper.le(StatisticsArea::getEntryRate, params.getEntryRateMax());
        }
        if (params.getThisDayJobCountMin() != null) {
            queryWrapper.ge(StatisticsArea::getThisDayJobCount, params.getThisDayJobCountMin());
        }
        if (params.getThisDayJobCountMax() != null) {
            queryWrapper.le(StatisticsArea::getThisDayJobCount, params.getThisDayJobCountMax());
        }
        if (params.getThisMonthJobCountMin() != null) {
            queryWrapper.ge(StatisticsArea::getThisMonthJobCount, params.getThisMonthJobCountMin());
        }
        if (params.getThisMonthJobCountMax() != null) {
            queryWrapper.le(StatisticsArea::getThisMonthJobCount, params.getThisMonthJobCountMax());
        }
        if (params.getThisYearJobCountMin() != null) {
            queryWrapper.ge(StatisticsArea::getThisYearJobCount, params.getThisYearJobCountMin());
        }
        if (params.getThisYearJobCountMax() != null) {
            queryWrapper.le(StatisticsArea::getThisYearJobCount, params.getThisYearJobCountMax());
        }
        if (params.getTotalJobCountMin() != null) {
            queryWrapper.ge(StatisticsArea::getTotalJobCount, params.getTotalJobCountMin());
        }
        if (params.getTotalJobCountMax() != null) {
            queryWrapper.le(StatisticsArea::getTotalJobCount, params.getTotalJobCountMax());
        }
        int two = 2;
        int three = 3;
        int four = 4;
        if (params.getOrderBy() != null) {
            if (params.getOrderBy() == 0) {
                queryWrapper.orderByDesc(StatisticsArea::getResumeCount);
            } else if (params.getOrderBy() == 1) {
                queryWrapper.orderByDesc(StatisticsArea::getResumeCount);
            } else if (params.getOrderBy() == two) {
                queryWrapper.orderByDesc(StatisticsArea::getFeedbackRate);
            } else if (params.getOrderBy() == three) {
                queryWrapper.orderByDesc(StatisticsArea::getViewRate);
            } else if (params.getOrderBy() == four) {
                queryWrapper.orderByDesc(StatisticsArea::getEntryRate);
            }
        } else {
            queryWrapper.orderByDesc(StatisticsArea::getResumeCount);
        }
        IPage<StatisticsArea> page = service.page(params.page(), queryWrapper);
        //分页结果转换器
        Function<StatisticsArea, StatisticsAreaVO> fun = statisticsIndustry -> statisticsAreaConvert.toVO(statisticsIndustry);

        return Result.ok((Page) page.convert(fun));
    }

    /**
     * 根据id查询
     *
     * @param id 数据id
     * @return
     */
    @GetMapping(value = "/queryById")
    @AutoLog(value = "地区数据查询",logType = 2,ty = "visit")
    @ApiOperation(value = "根据id查询", notes = "根据id查询")
    @PermissionData
    public Result<StatisticsAreaVO> queryById(String id) {
        StatisticsArea statisticsArea = statisticsAreaService
                .lambdaQuery()
                .eq(StatisticsArea::getId, id)
                .eq(StatisticsArea::getDelFlag, false)
                .one();
        if (statisticsArea == null) {
            throw new JeecgBootException("数据不存在");
        }
        return Result.ok(statisticsAreaConvert.toVO(statisticsArea));
    }

    /**
     * 添加
     *
     * @param params 请求参数
     * @return
     */
    @PostMapping(value = "/add")
    @AutoLog(value = "地区数据添加",logType = 2,ty = "insert")
    @ApiOperation(value = "添加", notes = "添加")
    @RequiresPermissions("statisticsArea:add")
    public Result<?> add(@RequestBody @Valid StatisticsAreaParams params) {

        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        StatisticsArea statisticsArea = statisticsAreaConvert.toEntity(params);
        statisticsArea.setCreateBy(sysUser.getId());
        statisticsArea.setCreateTime(DateUtil.date());
        statisticsArea.setDelFlag(false);

        //statisticsArea.setNo(super.sort("statistics_area", Constant.NO_SEED));//FIXME: 编号,可根据情况隐藏
        boolean saved = statisticsAreaService.save(statisticsArea);
        return saved ? Result.ok() : Result.error("添加失败");
    }

    /**
     * 编辑
     *
     * @param params 请求参数
     * @return
     */
    @PostMapping(value = "/edit")
    @AutoLog(value = "地区数据编辑",logType = 2,ty = "update")
    @ApiOperation(value = "编辑", notes = "编辑")
    @RequiresPermissions("statisticsArea:edit")
    public Result<?> edit(@RequestBody @Valid StatisticsAreaParams params) {
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();

        StatisticsArea statisticsArea = statisticsAreaConvert.toEntity(params);
        statisticsArea.setUpdateBy(sysUser.getId());
        statisticsArea.setUpdateTime(DateUtil.date());

        boolean updated = statisticsAreaService.updateById(statisticsArea);
        return updated ? Result.ok() : Result.error("编辑失败");
    }


    /**
     * 批量删除
     *
     * @param id 数据id
     * @return
     */
    @GetMapping(value = "/delete")
    @AutoLog(value = "地区数据删除",logType = 2,ty = "delete")
    @ApiOperation(value = "批量删除", notes = "批量删除")
    @RequiresPermissions("statisticsArea:delete")
    public Result delete(String[] id) {
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        LambdaUpdateWrapper<StatisticsArea> updateWrapper = Wrappers.lambdaUpdate();
        updateWrapper.in(StatisticsArea::getId, id)
                .set(StatisticsArea::getDelFlag, true)
                .set(StatisticsArea::getUpdateBy, sysUser.getUsername())
                .set(StatisticsArea::getUpdateTime, DateUtil.date());

        boolean deleted = statisticsAreaService.update(updateWrapper);
        return deleted ? Result.ok() : Result.error("删除失败");
    }

    /**
     * 属性值是否重复
     *
     * @return
     */
    @Override
    @GetMapping("/propertyRepeat")
    @ApiOperation(value = "属性值是否重复", notes = "属性值是否重复")
    public Result propertyIsRepeat(@Valid AttributesValueParams params) {
        return super.propertyIsRepeat(params);
    }

    /**
     * 导出excel
     *
     * @param params
     */
    @SneakyThrows
    @GetMapping(value = "/exportXls")
    @ApiOperation(value = "导出excel", notes = "导出excel")
    public void exportXls(HttpServletResponse response, StatParams params) {
        LambdaQueryWrapper<StatisticsArea> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.ne(StatisticsArea::getLevel, "province");
        if (StringUtils.isNotBlank(params.getId())) {
            String id = params.getId();
            String dou = ",";
            if (id.contains(dou)) {
                queryWrapper.in(StatisticsArea::getId, id
                        .split(","));
            } else {
                queryWrapper.in(StatisticsArea::getId, id
                );
            }
        }
        if (StringUtils.isNotBlank(params.getName())) {
            queryWrapper.like(StatisticsArea::getName, params.getName());
        }
        if (params.getReCountMin() != null) {
            queryWrapper.ge(StatisticsArea::getResumeCount, params.getReCountMin());
        }
        if (params.getReCountMax() != null) {
            queryWrapper.le(StatisticsArea::getResumeCount, params.getReCountMax());
        }
        if (params.getFeedbackRateMin() != null) {
            queryWrapper.ge(StatisticsArea::getFeedbackRate, params.getFeedbackRateMin());
        }
        if (params.getFeedbackRateMax() != null) {
            queryWrapper.le(StatisticsArea::getFeedbackRate, params.getFeedbackRateMax());
        }
        if (params.getViewRateMin() != null) {
            queryWrapper.ge(StatisticsArea::getViewRate, params.getViewRateMin());
        }
        if (params.getViewRateMax() != null) {
            queryWrapper.le(StatisticsArea::getViewRate, params.getViewRateMax());
        }
        if (params.getEntryRateMin() != null) {
            queryWrapper.ge(StatisticsArea::getEntryRate, params.getEntryRateMin());
        }
        if (params.getEntryRateMax() != null) {
            queryWrapper.le(StatisticsArea::getEntryRate, params.getEntryRateMax());
        }
        if (params.getThisDayJobCountMin() != null) {
            queryWrapper.ge(StatisticsArea::getThisDayJobCount, params.getThisDayJobCountMin());
        }
        if (params.getThisDayJobCountMax() != null) {
            queryWrapper.le(StatisticsArea::getThisDayJobCount, params.getThisDayJobCountMax());
        }
        if (params.getThisMonthJobCountMin() != null) {
            queryWrapper.ge(StatisticsArea::getThisMonthJobCount, params.getThisMonthJobCountMin());
        }
        if (params.getThisMonthJobCountMax() != null) {
            queryWrapper.le(StatisticsArea::getThisMonthJobCount, params.getThisMonthJobCountMax());
        }
        if (params.getThisYearJobCountMin() != null) {
            queryWrapper.ge(StatisticsArea::getThisYearJobCount, params.getThisYearJobCountMin());
        }
        if (params.getThisYearJobCountMax() != null) {
            queryWrapper.le(StatisticsArea::getThisYearJobCount, params.getThisYearJobCountMax());
        }
        if (params.getTotalJobCountMin() != null) {
            queryWrapper.ge(StatisticsArea::getTotalJobCount, params.getTotalJobCountMin());
        }
        if (params.getTotalJobCountMax() != null) {
            queryWrapper.le(StatisticsArea::getTotalJobCount, params.getTotalJobCountMax());
        }
        int f = 2;
        int g= 3;
        if (params.getOrderBy() != null) {
            if (params.getOrderBy() == 0) {
                queryWrapper.orderByDesc(StatisticsArea::getResumeCount);
            } else if (params.getOrderBy() == 1) {
                queryWrapper.orderByDesc(StatisticsArea::getFeedbackRate);
            }
            if (params.getOrderBy() == f) {
                queryWrapper.orderByDesc(StatisticsArea::getViewRate);
            }
            if (params.getOrderBy() == g) {
                queryWrapper.orderByDesc(StatisticsArea::getEntryRate);
            }
        } else {
            queryWrapper.orderByDesc(StatisticsArea::getResumeCount);
        }
        List<StatisticsArea> data = service.list(queryWrapper);
        ExcelUtil<StatisticsAreaVO> excelUtil = new ExcelUtil();
        excelUtil.setClose(false);
        excelUtil.buildExcel(response, statisticsAreaConvert.toVO(data));
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @PostMapping(value = "/importExcel")
    @ApiOperation(value = "通过excel导入数据", notes = "通过excel导入数据")
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, StatisticsArea.class);
    }

}
